package com.cxh.controller;

import static com.cxh.constant.SsmConstant.*;

import com.cxh.entity.User;
import com.cxh.enums.ExceptionInfoEnum;
import com.cxh.exception.SsmException;
import com.cxh.service.UserService;
import com.cxh.util.SendSMSUtil;
import com.cxh.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

@Slf4j
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    public UserService userService;

    @Autowired
    public SendSMSUtil sendSMSUtil;

    //1、转发到注册页面
    @GetMapping("/register-ui")
    public String registerUI() {
        return "user/register";
    }

    //2、异步校验用户名
    @PostMapping("/check-username")
    @ResponseBody
    public ResultVo checkUsername(@RequestBody User user) {

        userService.checkUsername(user.getUsername());

        return new ResultVo(0, "成功", null);
    }

    //3、发送手机验证码
    @PostMapping("/send-sms")
    @ResponseBody
    public ResultVo sendSMS(String phone, HttpSession session) {
        //1、接收参数
        //2、校验参数.
        if (phone == null || phone.trim().length() < 11) {
            log.info("【发送短信验证】 手机号格式不正确 phone = {}", phone);
            throw new SsmException(21, "手机号格式不正确");
        }
        //3、如果没进if说明手机号正确,则发送验证码
        ResultVo vo = sendSMSUtil.sendSMS(phone, session);
        //4、响应数据
        return vo;
    }

    //4、注册
    @PostMapping("/register")
    @ResponseBody
    public ResultVo register(@Valid User user, BindingResult bindingResult, String registerCode, HttpSession session) {
        //1、校验验证码是否正确
        String trueCode = (String) session.getAttribute(USER_CODE);
        if (!registerCode.equals(trueCode) || registerCode == null) {
            log.error("【执行注册】 验证码不正确!! registerCode = {},trueCode = {}", registerCode, trueCode);
            throw new SsmException(ExceptionInfoEnum.CAPTCHA_ERROR);
        }
        //2、判断用户名，密码，手机号是否合法
        if (bindingResult.hasErrors()) {
            String msg = bindingResult.getFieldError().getDefaultMessage();
            log.error("【执行注册】 参数不合法 , msg = {},user = {}", msg, user);
            throw new SsmException(ExceptionInfoEnum.PARAM_ERROR);
        }

        //3、执行注册
        userService.register(user);
        return new ResultVo(0, "成功", null);
    }

    //5. 跳转到登录页面
    @GetMapping("/login-ui")
    public String loginUI() {
        return "user/login";
    }

    //6、登录功能
    @PostMapping("/login")
    @ResponseBody
    public ResultVo login(String username, String password, HttpSession session) {
        if (username == null || username.trim().length() == 0 || password == null || password.trim().length() == 0) {
            log.info("【登录功能】 参数不合法!! username = {},password = {}", username, password);
            throw new SsmException(ExceptionInfoEnum.PARAM_ERROR.getCode(), "用户名和密码均为必填项,岂能不填!");
        }
        User user = userService.login(username, password);
        session.setAttribute(USER_INFO, user);

        return new ResultVo(0, "成功", null);
    }

}
